package com.cos.web.controller;

import com.cos.entity.User;
import com.cos.util.JedisPoolUtil;
import com.cos.util.RandomUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.omg.CORBA.OMGVMCID;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * sso 解决方案
 * 单点登录
 */
@Controller
@RequestMapping("/jedis")
public class JedisController {
    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        System.out.println("wahh");
        return "wahaha";
    }

    /*
        不符合分布式系统的登录验证
     */
    @RequestMapping("/login_old")
    @ResponseBody
    public String login_old(String username, String password, HttpSession session) {
        String uname = "admin";
        String pwd = "admin";
        if (uname.equals(username) && pwd.equals(password)) {
            User u = new User(uname, 18, "男");
            session.setAttribute("is_login", u);
            return "OK";
        }
        return "NO";
    }

    @RequestMapping("/chenk_login_old")
    @ResponseBody
    public String chenk_login_old(HttpSession session) {
        User is_login = (User) session.getAttribute("is_login");
        if (is_login != null) {
            return "OK is login";
        }
        return "NO is not login";
    }

    /**
     * 使用redis来处理单点登录问题
     */
    @RequestMapping("/login_redis")
    @ResponseBody
    public String login_redis(String username, String password, HttpServletResponse response) throws JsonProcessingException {
        String uname = "admin";
        String pwd = "admin";
        if (uname.equals(username) && pwd.equals(password)) {
            User u = new User(uname, 18, "男");
            String userToken = RandomUtils.createOrderId();
            ObjectMapper om = new ObjectMapper();
            //redis保存用
            String stringUser = om.writeValueAsString(u);
            //创建cookie
            //保存rediskey 值
            //使用随机数 作为value 使用常量user_cookie作为key
            Cookie c = new Cookie("user_cookie", userToken);
            c.setPath("/");
            c.setMaxAge(60 * 60 * 24 * 7);
            response.addCookie(c);
            //2 保存数据到redis
            String setex = JedisPoolUtil.setex(userToken, stringUser, 60 * 60 * 24 * 7);
            System.out.println("redis保存:" + setex);
            return "OK";
        }
        return "NO";
    }

    @RequestMapping("/chenk_login_redis")
    @ResponseBody
    public String chenk_login_redis(@CookieValue(name = "user_cookie") String cookieValue, HttpServletRequest request) throws JsonProcessingException {
     /*   Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("user_cookie")) {
                cookie.getValue();
            }
        }*/
        String s = JedisPoolUtil.get(cookieValue);
        if (s != null) {
            ObjectMapper om = new ObjectMapper();
            //从redis中取值
            User user = om.readValue(s, User.class);
            System.out.println(user);
            return "OK is login_redis1";
        }
        return "NO is not login_redis";
    }


}
